package com.zycfc.zsf.boot.distributedlock.zk.util;

import org.I0Itec.zkclient.*;
import com.zycfc.zsf.boot.distributedlock.configuration.*;
import com.zycfc.zsf.boot.util.text.*;
import org.I0Itec.zkclient.serialize.*;
import java.util.*;
import org.slf4j.*;

public class ZkClientUtil
{
    private static final Logger log;
    private static ZkClient zkClient;
    private static String zkServers;
    private static int sessionTimeout;
    private static int connectionTimeout;
    
    private static synchronized void initZkClient() {
        String zkServers = DistributedLockProperties.getZkAddress();
        zkServers = (StringUtils.isEmpty(zkServers) ? ZkClientUtil.zkServers : zkServers);
        ZkClientUtil.zkClient = new ZkClient(zkServers, ZkClientUtil.sessionTimeout, ZkClientUtil.connectionTimeout, (ZkSerializer)new SerializableSerializer());
    }
    
    public static ZkClient getNewZkClient() {
        String zkServers = DistributedLockProperties.getZkAddress();
        zkServers = (StringUtils.isEmpty(zkServers) ? ZkClientUtil.zkServers : zkServers);
        return new ZkClient(zkServers, ZkClientUtil.sessionTimeout, ZkClientUtil.connectionTimeout, (ZkSerializer)new SerializableSerializer());
    }
    
    public static ZkClient getNewZkClient(final String tmpZkServers) {
        return new ZkClient(tmpZkServers, ZkClientUtil.sessionTimeout, ZkClientUtil.connectionTimeout, (ZkSerializer)new SerializableSerializer());
    }
    
    public static void createIfNotExists(final String path) {
        if (ZkClientUtil.zkClient.exists(path)) {
            return;
        }
        ZkClientUtil.zkClient.createPersistent(path);
    }
    
    public static String createEphemeralSequential(final String path) {
        return ZkClientUtil.zkClient.createEphemeralSequential(path, (Object)null);
    }
    
    public static List<String> getChildNodeList(final String path) {
        return (List<String>)ZkClientUtil.zkClient.getChildren(path);
    }
    
    public static void delete(final String path) {
        ZkClientUtil.zkClient.delete(path);
    }
    
    public static void createIfNotExists(final ZkClient zkClient, final String path) {
        if (zkClient.exists(path)) {
            return;
        }
        zkClient.createPersistent(path, true);
    }
    
    public static String createEphemeralSequential(final ZkClient zkClient, final String path) {
        return zkClient.createEphemeralSequential(path, (Object)null);
    }
    
    public static List<String> getChildNodeList(final ZkClient zkClient, final String path) {
        return (List<String>)zkClient.getChildren(path);
    }
    
    public static boolean delete(final ZkClient zkClient, final String path) {
        return zkClient.delete(path);
    }
    
    static {
        log = LoggerFactory.getLogger((Class)ZkClientUtil.class);
        ZkClientUtil.zkServers = "52.80.16.142:2181,52.80.16.142:2182,52.80.16.142:2183";
        ZkClientUtil.sessionTimeout = 5000;
        ZkClientUtil.connectionTimeout = 5000;
    }
}
